package com.itheima.api;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.GetOrderDto;
import com.itheima.dto.OmsOrderDto;
import com.itheima.dto.deliveryDto;
import com.itheima.dto.MoneyInfoParam;
import com.itheima.dto.receiverInfoParam;
import com.itheima.mapper.OmsOrderItemMapper;
import com.itheima.mapper.OmsOrderMapper;
import com.itheima.mapper.OmsOrderOperateHistoryMapper;
import com.itheima.pojo.OmsOrder;
import com.itheima.pojo.OmsOrderItem;
import com.itheima.pojo.OmsOrderOperateHistory;
import com.itheima.vo.MinioVo;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 芝士雪豹
 * @create 2022/10/13/20:36
 */
@DubboService
public class OmsOrderAPiImpl implements OmsOrderApi {
    @Autowired
    private OmsOrderMapper omsOrderMapper;

    @Autowired
    private OmsOrderOperateHistoryMapper omsOrderOperateHistoryMapper;

    @Autowired
    private OmsOrderItemMapper omsOrderItemMapper;

    @Override
    public MinioVo delete(Integer[] ids) {
        MinioVo vo = new MinioVo();
        for (Integer id : ids) {
            if (omsOrderMapper.deleteById(id) == 0) {
                vo.setCode(404);
                vo.setDate(null);
                vo.setMessage("暂无数据");
                return vo;
            }
        }
        vo.setMessage("删除成功");
        vo.setDate(null);
        vo.setCode(200);
        return vo;
    }

    @Override
    public PageResult findOrder(OmsOrderDto dto) {
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getCreateTime() != null, OmsOrder::getCreateTime, dto.getCreateTime())
                .eq(dto.getOrderSn() != null, OmsOrder::getOrderSn, dto.getOrderSn())
                .eq(dto.getOrderType() != null, OmsOrder::getOrderType, dto.getOrderType())
                .eq(dto.getReceiveKeyword() != null, OmsOrder::getReceiverPhone, dto.getReceiveKeyword())
                .eq(dto.getSourceType() != null, OmsOrder::getSourceType, dto.getSourceType())
                .eq(dto.getStatus() != null, OmsOrder::getStatus, dto.getStatus());
        Page pageInfo = new Page<>(dto.getPageNum(), dto.getPageSize());
        omsOrderMapper.selectPage(pageInfo, queryWrapper);
        return new PageResult<>(Convert.toInt(pageInfo.getCurrent()), Convert.toInt(pageInfo.getSize()), Convert.toInt(pageInfo.getTotal()), Convert.toInt(pageInfo.getCurrent()), pageInfo.getRecords());

    }

    @Override
    public MinioVo close(Integer[] ids, String note) {
        LambdaUpdateWrapper<OmsOrder> wrapper = new LambdaUpdateWrapper<OmsOrder>();
        for (Integer id : ids) {
            wrapper.eq(id != null, OmsOrder::getId, id)
                    .set(note != null, OmsOrder::getNote, note)
                    .set(OmsOrder::getStatus, 4);
        }
        int update = omsOrderMapper.update(null, wrapper);
        MinioVo vo = new MinioVo();
        if (update != 0) {
            vo.setCode(200);
            vo.setDate(null);
            vo.setMessage("关闭成功");
        }
        vo.setCode(404);
        vo.setDate(null);
        vo.setMessage("暂无数据可以关闭");

        return vo;
    }

    @Override
    public MinioVo delivery(deliveryDto dto) {
        LambdaUpdateWrapper<OmsOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(dto.getDeliverySn() != null, OmsOrder::getDeliverySn, dto.getDeliverySn())
                .eq(dto.getDeliveryCompany() != null, OmsOrder::getDeliveryCompany, dto.getDeliveryCompany())
                .eq(dto.getOrderId() != null, OmsOrder::getId, dto.getOrderId())
                .set(OmsOrder::getStatus, 2);
        int update = omsOrderMapper.update(null, wrapper);
        MinioVo vo = new MinioVo();
        if (update != 0) {
            vo.setCode(200);
            vo.setDate(null);
            vo.setMessage("发货成功");
        }
        vo.setCode(404);
        vo.setDate(null);
        vo.setMessage("暂无商品可发货");
        return vo;
    }

    @Override
    public MinioVo moneyInfo(MoneyInfoParam moneyInfoParam) {
        LambdaUpdateWrapper<OmsOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(moneyInfoParam.getStatus() != null, OmsOrder::getStatus, 0)
                .eq(moneyInfoParam.getOrderId() != null, OmsOrder::getId, moneyInfoParam.getOrderId())
                .set(moneyInfoParam.getFreightAmount() != null, OmsOrder::getFreightAmount, moneyInfoParam.getDiscountAmount())
                .set(moneyInfoParam.getDiscountAmount() != null, OmsOrder::getDiscountAmount, moneyInfoParam.getDiscountAmount());
        int update = omsOrderMapper.update(null, wrapper);
        MinioVo vo = new MinioVo();
        if (update != 0) {
            vo.setCode(200);
            vo.setDate(null);
            vo.setMessage("修改金额成功");
        }
        vo.setCode(404);
        vo.setDate(null);
        vo.setMessage("暂无金额更改");
        return vo;
    }

    @Override
    public MinioVo note(Integer id, String note, Integer status) {
        LambdaUpdateWrapper<OmsOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(status != null, OmsOrder::getStatus, status)
                .eq(id != null, OmsOrder::getId, id)
                .set(note != null, OmsOrder::getNote, note);
        int update = omsOrderMapper.update(null, wrapper);
        MinioVo vo = new MinioVo();
        if (update != 0) {
            vo.setCode(200);
            vo.setDate(null);
            vo.setMessage("修改备注成功");
        }
        vo.setCode(404);
        vo.setDate(null);
        vo.setMessage("暂无备注更改");
        return vo;
    }

    @Override
    public MinioVo receiverInfo(receiverInfoParam param) {
        LambdaUpdateWrapper<OmsOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(param.getOrderId() != null, OmsOrder::getId, param.getOrderId())
                .eq(param.getStatus() != null, OmsOrder::getStatus, 0)
                .set(param.getReceiverCity() != null, OmsOrder::getReceiverCity, param.getReceiverCity())
                .set(param.getReceiverName() != null, OmsOrder::getReceiverName, param.getReceiverName())
                .set(param.getReceiverPhone() != null, OmsOrder::getReceiverPhone, param.getReceiverPhone())
                .set(param.getReceiverDetailAddress() != null, OmsOrder::getReceiverDetailAddress, param.getReceiverDetailAddress())
                .set(param.getReceiverPostCode() != null, OmsOrder::getReceiverPostCode, param.getReceiverPostCode())
                .set(param.getReceiverProvince() != null, OmsOrder::getReceiverProvince, param.getReceiverProvince())
                .set(param.getReceiverRegion() != null, OmsOrder::getReceiverRegion, param.getReceiverRegion());
        int update = omsOrderMapper.update(null, wrapper);
        MinioVo vo = new MinioVo();
        if (update != 0) {
            vo.setCode(200);
            vo.setDate(null);
            vo.setMessage("修改订单用户成功");
        }
        vo.setCode(404);
        vo.setDate(null);
        vo.setMessage("暂无用户更改");
        return vo;

    }

    @Override
    public GetOrderDto getOrder(Integer id) {
        OmsOrder omsOrder = omsOrderMapper.selectById(id);
        GetOrderDto omsOrderDto = new GetOrderDto();
        if(omsOrder != null) {
            BeanUtil.copyProperties(omsOrder, omsOrderDto);
            List<OmsOrderOperateHistory> omsOrderOperateHistoryList = omsOrderOperateHistoryMapper.selectList(new LambdaQueryWrapper<OmsOrderOperateHistory>().eq(OmsOrderOperateHistory::getOrderId, omsOrder.getId()));
            List<OmsOrderItem> omsOrderItemList = omsOrderItemMapper.selectList(new LambdaQueryWrapper<OmsOrderItem>().eq(OmsOrderItem::getOrderId, omsOrder.getId()));
            omsOrderDto.setHistoryList(omsOrderOperateHistoryList);
            omsOrderDto.setOrderItemList(omsOrderItemList);
        }
        return omsOrderDto;
    }


}
